using System.Windows;
using MySql.Data.MySqlClient;
using WpfApp1.Models;

namespace WpfApp1.DataAccess;

public class TeacherCourseDataAccess : ITeacherCourseDataAccess
{
        // 查询自己的课程信息(教师)
    public List<Course> GetTeacherCourseByNumber(string teacherNumber)
    {
        var courses = new List<Course>();
        using (var connection = SqlAccess.CreateConnection())
        {
            connection.Open();
            var command = new MySqlCommand($"SELECT * FROM courses WHERE teacher_number='{teacherNumber}'", connection);
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    courses.Add(new Course(
                        reader.GetInt32("course_id"),
                        reader.GetString("course_number"),
                        reader.GetString("course_name"),
                        reader.GetInt32("credits"),
                        reader.GetInt32("hours"),
                        reader.GetString("teacher_number")
                    ));
                }
            }
        }
        return courses;
    }

    // 给学生成绩分数(教师)
    public void AddStudentCourse(CourseSelection courseSelection)
    {
        using (var connection = SqlAccess.CreateConnection())
        {
            connection.Open();
            var sql = $"INSERT INTO course_selection values ('{courseSelection.StudentNumber}','{courseSelection.CourseNumber}',{courseSelection.Grades})";
            var command = new MySqlCommand(sql, connection);
            var row = command.ExecuteNonQuery();
            MessageBox.Show(row == 1 ? "评分成功！" : "评分失败！");
        }
    }

    // 修改学生课程评分(教师)
    public void UpdateStudentCourse(CourseSelection courseSelection)
    {
        using (var connection = SqlAccess.CreateConnection())
        {
            connection.Open();
            var sql = $"UPDATE course_selection SET grades={courseSelection.Grades} WHERE student_number='{courseSelection.StudentNumber}' and course_number='{courseSelection.CourseNumber}'";
            var command = new MySqlCommand(sql, connection);
            var row = command.ExecuteNonQuery();
            MessageBox.Show(row == 1 ? "修改成绩成功！" : "修改成绩失败！");
        }
    }
}